package edu.xidian.bdilab.jrbigdata.service.government.register.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.xidian.bdilab.jrbigdata.domain.GovernmentInfo;
import edu.xidian.bdilab.jrbigdata.domain.GovernmentUser;
import edu.xidian.bdilab.jrbigdata.mapper.GovermentUserMapper;
import edu.xidian.bdilab.jrbigdata.mapper.GovernmentInfoMapper;
import edu.xidian.bdilab.jrbigdata.service.government.register.GovernmentRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName GoveRegisterServiceImpl
 * @Description TODO
 * @Author tuantuan
 * @Date 2019/3/29 下午7:28
 * @Version 1.0
 * @Attention Copyright (C), 2004-2019, BDILab, XiDian University
 **/
@Service
public class GoveRegisterServiceImpl implements GovernmentRegisterService {
    @Autowired
    GovernmentInfoMapper governmentInfoMapper;
    @Autowired
    GovermentUserMapper govermentUserMapper;


    /**
     * 查询用户是否已经注册
     * @param fkUserName
     * @return
     */
    @Override
    public boolean checkUserName(String fkUserName) {
        QueryWrapper<GovernmentUser> userQueryWrapper = new QueryWrapper<>();

        userQueryWrapper.eq("user_name", fkUserName);
        GovernmentUser governmentUser = govermentUserMapper.selectOne(userQueryWrapper);
        if (governmentUser == null){
            return true;
        }
        return false;
    }

    /**
     * 通过机构代码查看是否重复注册
     * @param govrenmentCode
     * @return
     */
    @Override
    public boolean checkGovernmentCode(int govrenmentCode) {
        QueryWrapper<GovernmentInfo> infoQueryWapper = new QueryWrapper<>();

        infoQueryWapper.eq("government_code", govrenmentCode);
        GovernmentInfo governmentInfo = governmentInfoMapper.selectOne(infoQueryWapper);
        if (governmentInfo == null){
            return true;
        }
        return false;
    }

    @Override
    public void setGovernmentInfo(int govrenmentCode, String fkUserName, String departmentName, String principalName, String principalPhone) {
        GovernmentInfo governmentInfo = new GovernmentInfo();
        governmentInfo.setGovernmentCode(govrenmentCode);
        governmentInfo.setFkUserName(fkUserName);
        governmentInfo.setDepartmentName(departmentName);
        governmentInfo.setPrincipalName(principalName);
        governmentInfo.setPrincipalPhone(principalPhone);

        governmentInfoMapper.insert(governmentInfo);
    }

    @Override
    public void setGovernmentUser(String fkUserName, String password) {
        GovernmentUser governmentUser = new GovernmentUser();
        governmentUser.setUserName(fkUserName);
        governmentUser.setPassword(password);

        govermentUserMapper.insert(governmentUser);
    }
}
